package com.hdax.easybuy.controller;

import com.hdax.easybuy.api.ProductManagerClient;
import com.hdax.easybuy.api.orderAPI.OrderDetailManagerClient;
import com.hdax.easybuy.api.orderAPI.OrderManagerClient;
import com.hdax.easybuy.entity.Order;
import com.hdax.easybuy.entity.OrderDetail;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/houtai")
public class HouTaiController {
    @Resource
    private OrderManagerClient orderManagerClient;
    @Resource
    private OrderDetailManagerClient orderDetailManagerClient;

    @Resource
    private ProductManagerClient productManagerClient;
    @RequestMapping("/queryOrder")
    @ResponseBody
    public List<Order> queryOrder(){
        List<Order> orderList = orderManagerClient.queryAllOrder();
        for (Order oo : orderList) {
            oo.setOrderDetailList(orderDetailManagerClient.getOrderDetailByOrderIdManager(oo.getId()));
            for (OrderDetail od : oo.getOrderDetailList()){
                od.setProduct(productManagerClient.queryById(od.getProductId()));
            }
        }

        return orderList;

    }

    @RequestMapping("/queryOrderById/{id}")
    @ResponseBody
    public List<Order> queryOrderByID(@PathVariable("id") int id){
        List<Order> orderList = orderManagerClient.getOrderByUserId(id);
        for (Order oo : orderList) {
            oo.setOrderDetailList(orderDetailManagerClient.getOrderDetailByOrderIdManager(oo.getId()));
            for (OrderDetail od : oo.getOrderDetailList()){
                od.setProduct(productManagerClient.queryById(od.getProductId()));
            }
        }

        return orderList;

    }
    @RequestMapping("/queryOrder1")
    //@ResponseBody
    public String queryOrder1(Model model){
        List<Order> orderList = orderManagerClient.queryAllOrder();
        for (Order oo : orderList) {
            oo.setOrderDetailList(orderDetailManagerClient.getOrderDetailByOrderIdManager(oo.getId()));
            for (OrderDetail od : oo.getOrderDetailList()){
                od.setProduct(productManagerClient.queryById(od.getProductId()));
            }
        }
        model.addAttribute("orderList",orderList);

        return "backend/order/orderList";

    }
    @RequestMapping("/queryOrder1Byid")
    //@ResponseBody
    public String queryOrder1ById(Model model,int id){
        List<Order> orderList = orderManagerClient.getOrderByUserId(id);
        for (Order oo : orderList) {
            oo.setOrderDetailList(orderDetailManagerClient.getOrderDetailByOrderIdManager(oo.getId()));
            for (OrderDetail od : oo.getOrderDetailList()){
                od.setProduct(productManagerClient.queryById(od.getProductId()));
            }
        }
        model.addAttribute("orderList",orderList);

        return "backend/order/orderList";

    }







    @RequestMapping("/queryOrderDetailAll/{OrderId}")
    @ResponseBody
    public List<OrderDetail> getAllOrderDetail(@PathVariable("OrderId") int OrderId){

        return orderDetailManagerClient.getOrderDetailByOrderIdManager(OrderId);
    }

    @RequestMapping("/queryOrderAll")
    @ResponseBody
    public List<Order> getAllOrder(){

        return orderManagerClient.queryAllOrder();
    }


    @RequestMapping("/test")
    //@ResponseBody
    public String test(){

        return "1";
    }



}
